<?php

namespace App\Http\Controllers\Api;

use App;
use App\Util\Common;
use App\Util\GetMac;
use App\Util\SqlServerUtil;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use function Symfony\Component\Debug\header;
use Illuminate\Support\Facades\Storage;
use Excel;

class JiaChunController extends CommonController
{
    protected $response = [
    		"Error" => 0,
    		"Message" => "Success!",
    		"Data" => []
	];
    
    //常规主燃油 
    public function cgMainjiachun(Request $request){
    	//fobjhide
    	$FRepKey = $request ->fobjectid ; //
    	// gz_YjiachunValue 值
    	$detail = SqlServerUtil::execToSql($this->SqlServer, "select fkey , fvalue from  gz_YjiachunValue   where fkey = 'CMCR' and fobjectid ='{$FRepKey}' " );
    	
    	showMsg(0,'成功',$detail);
    } 
    
    
    //常规主燃油
    public function cgMainjiachunSave(Request $request){
    	//fobjhide
    	$FRepKey = $request ->fobjectid ; //
    	$fvalue = $request ->cmcr ;
    	//
    	$detail = SqlServerUtil::execToSql($this->SqlServer, "select fkey , fvalue  from  gz_YjiachunValue   where fkey = 'CMCR' and fobjectid ='{$FRepKey}' ",2 );
    	if(empty($detail)){
    		SqlServerUtil::sqlsrvCreateTosql( $this->SqlServer, 'gz_YjiachunValue',['fkey'=>'CMCR' ,'fvalue'=> $fvalue , 'fobjectid'=> $FRepKey ]  ) ; 
    	}else{
	    	// gz_YjiachunValue 值
	    	SqlServerUtil::execToSql($this->SqlServer, "update gz_YjiachunValue set fvalue = {$fvalue} where fkey = 'CMCR' and fobjectid ='{$FRepKey}' " );
    	}
    	
    	showMsg(0,'成功');
    } 
  
    /**
    * //燃油汇总    Save
    * @date: 2022年12月29日 上午10:05:50
    * @author: juyi
    */
    public function jiachunhuizongSave(Request $request){    	
    	$data  = $request ->all() ; 
    	$fjiachunValue = $request ->cmcr ;
    	// 首机
    	$shouji = $data['shouji'] ; 
    	
    	// 非首机
    	$noshouji = $data['noshouji'] ; 
    	if(empty($noshouji) || empty($shouji) ){
    		showMsg(1,'数据不能为空');
    	}
    	$db = ' gz_YjiachunHuizong ' ; 
    	// 启动事务
    	if (sqlsrv_begin_transaction($this->SqlServer) === false) {
    		die(print_r(sqlsrv_errors(), true));
    	}
    	
    	//先删 后插入
    	$del = SqlServerUtil::sqlexecTosql($this->SqlServer, "delete from {$db} ");
    	
    	foreach ($shouji as $k =>&$v)
    	{
    		$v['fjiachunValue'] = $fjiachunValue ;
    	}
    	
    	foreach ($noshouji as $k1 =>&$v1)
    	{
    		$v['fjiachunValue'] = $fjiachunValue ;
    		$v['ftype'] = 1 ;
    	}
    	
    	$res1 = SqlServerUtil::sqlsrvAllTosql( $this->SqlServer, 'gz_YjiachunValue', $shouji  ) ;
    	
    	$res2 = SqlServerUtil::sqlsrvAllTosql( $this->SqlServer, 'gz_YjiachunValue',$noshouji ) ;
    	if($del && $res1 && $res2  ){
    		sqlsrv_commit($this->SqlServer);
    		showMsg(200,'成功');
    	} else {
    		sqlsrv_rollback($this->SqlServer);
    		showMsg(400,'失败');
    	}
    	
    }
    
    
    
    /**
    * 燃油 表   表格 
    * @date: 2022年12月29日 下午3:18:10
    * @author: juyi
    */
    public function jiachunList(Request $request){
    	$FRepKey = $request ->fobjectid ; //
    	if($FRepKey == 29010){
    		$sql =" select *  from gz_YjiachunHz where  fobjectid ='{$FRepKey}' " ;
    	}else{
	    	$sql =" select *  from gz_Yjiachun where  fobjectid ='{$FRepKey}' " ; 
    	}
    	
    	$res = SqlServerUtil::execTosqlOK($this->SqlServer, $sql ) ;
    	$result1=[];
    	if($res){
    		$result1 = arraykeyToLower($res);
    		foreach ($result1 as $k => &$v)
    		{
    			foreach ($v as $k1=>$v1)
    			{
    				if( $v[$k1] ==='.00' || $v[$k1] ==='.000' || $v[$k1] ==='.0000' || ($v[$k1]>0 && $v[$k1]<1) ){
    					$v[$k1] = floatval($v1);
    				}
    			}
    		}
    	}
    	$result = array_val_chunk($result1 ,'fsetkey' );
    	if($FRepKey == '29011'){
    		$Tmp = SqlServerUtil::execTosql($this->SqlServer , 'select u1.* from gz_YjiachunValue v1 INNER JOIN gz_YjiachunHZ u1 on v1.fbillno = u1.fbillno ' ,2);
    		$result['ranyou'][0]['fhry'] = 0 ;
    		$result['ranyou'][0]['fhqg'] = 0 ;
    		$result['ranyou'][0]['qtry'] = 0 ;
    		$result['ranyou'][0]['qtqg'] = 0 ;
    		$result['ranyou'][0]['naoh'] = 0 ;
    		
    		$result['jiachun'][0]['jcfh'] = 0 ;
    		$result['jiachun'][0]['jcfhqg'] = 0 ;
    		$result['jiachun'][0]['jcqtry'] = 0 ;
    		$result['jiachun'][0]['jcqtqg'] = 0 ;
    		$result['jiachun'][0]['jcyry'] = 0 ;
    		$result['jiachun'][0]['jcnaoh'] = 0 ;
    		if(!empty($Tmp)){
    			$result['ranyou'][0]['fhry'] = $Tmp['temp29011fhry'] ;
    			$result['ranyou'][0]['fhqg'] = $Tmp['temp29011fhqg'] ;
    			$result['ranyou'][0]['qtry'] = $Tmp['temp29011qtry'] ;
    			$result['ranyou'][0]['qtqg'] = $Tmp['temp29011qtqg'] ;
    			$result['ranyou'][0]['naoh'] = $Tmp['temp29011naoh'] ;
    			
    			$result['jiachun'][0]['jcfh'] = $Tmp['temp29011jcfh'] ;
    			$result['jiachun'][0]['jcfhqg'] = $Tmp['temp29011jcfhqg'] ;
    			$result['jiachun'][0]['jcqtry'] = $Tmp['temp29011jcqtry'] ;
    			$result['jiachun'][0]['jcqtqg'] = $Tmp['temp29011jcqtqg'] ;
    			$result['jiachun'][0]['jcyry'] = $Tmp['temp29011jcyry'] ;
    			$result['jiachun'][0]['jcnaoh'] = $Tmp['temp29011jcnaoh'] ;
    		}
    		
    	}
    	showMsg(0,'成功',$result ) ;
    	
    }
    
    /**
    * 燃油保存  
    * @date: 2022年12月29日 下午3:18:57
    * @author: juyi
    */
    public function jiachunSave(Request $request){
    	$FRepKey = $request ->fobjectid ; //
    	$fvalue = $request ->cmcr ;
    	$fbillno  = $request ->fbillno ; //
    	$fmodel  = $request ->fmodel ; //
    	
    	if(empty($fbillno) || empty($fmodel) ){
    		showMsg(400,'错误，请选择一行');
    	}
    	
    	// 自动生成燃油  29002
    	$request ->fobjectid =29002;
    	$controller = app('App\Http\Controllers\Api\RanyouController');
        $controller->ranyouSaveBack($request) ;
    	
    	
    	//更新   燃油值 
    	SqlServerUtil::execToSql($this->SqlServer, "update gz_YjiachunValue set fvalue = {$fvalue} , fbillno='{$fbillno}'  where fkey = 'CMCR' and fobjectid ='{$FRepKey}' " );
    	
    	//29011	 常规主燃油
    	// 磨合调整过程  -- 负荷运行  fmhfhyx
    	// 负荷升降估算  fmhfhsj
    	
    	// 内部提交和FAT过程  fnbfhyx
    	//负荷升降估算  fnbfhsj     //这两个 乘以  2 
    	
    	//增压器匹配和性能调整  fzyqxn
    	//其他运行（含故障/功能性试验/安保试验等）  fqtyx
    	
    	// 首台机燃油消耗总量（kg）  =   fqtyx +  fzyqxn  + (fnbfhyx + fnbfhsj)*2 +  fmhfhsj + fmhfhyx
    	// 后续机燃油消耗总量（kg）   =   fqtyx + (fnbfhyx + fnbfhsj)*2 +  fmhfhsj + fmhfhyx
    	$jisuan_0_001 = 0.001 ;
    	$min60 = 60;
    	$baseSql = "select ffuhe ,fsjtime ,fsjtime1, fpower,fyxtime,fyh,fmaxyh,fryxhl,ffuheval ,fsetkey,fmaxyhval,fobjectid,fnaoh,
		fnsxh,fyryxh,fqigangyh  from gz_yjiachunset order by fobjectid ,fsort " ;
    	$res = SqlServerUtil::execToSql($this->SqlServer,  $baseSql); 
    	
    	$temp =[];
    	$temp29011_ry = 0 ;     $temp29011_qg = 0 ; 
//     	$temp29011_fhry = 0; $temp29011_fhqg = 0;  $temp29011_qtry = 0;  $temp29011_qtqg = 0;  $temp29011_naoh = 0;
//     	$temp29011_fhjc = 0; $temp29011_jcfhqg = 0;  $temp29011_jcqtry = 0;  $temp29011_jcqtqg = 0;  $temp29011_jcryy = 0; $temp29011_jcnaoh = 0;
    	
    	$temp29012_ry = 0 ;    	$temp29012_ryno = 0 ;
    	$temp29012_qg = 0 ;    	$temp29012_qgno = 0 ;
    	$temp29012_naoh = 0 ;   $temp29012_naohno = 0 ;
    	
    	$temp29013_ry = 0 ;    	$temp29013_ryno = 0 ; 
    	$temp29013_qg = 0 ;     $temp29013_qgno = 0 ; 
    	$temp29013_naoh = 0 ;   $temp29013_naohno = 0 ; 
    
    	
    	$temp29014_ry = 0 ;    	$temp29014_ryno = 0 ;
    	$temp29014_qg = 0 ;    	$temp29014_qgno = 0 ;
    	$temp29014_naoh = 0 ;   $temp29014_naohno = 0 ;
    	$temp29014_yry = 0 ;    $temp29014_yryno = 0 ; 
    	
    	$temp29015_ry = 0 ;    	$temp29015_ryno = 0 ;
    	$temp29015_qg = 0 ;    	$temp29015_qgno = 0 ;
    	$temp29015_naoh = 0 ;   $temp29015_naohno = 0 ;
    	$temp29015_yry = 0 ;    $temp29015_yryno = 0 ; 
    	 
    	
    	$temp29012_ry0 = 0;    	$temp29012_ry1 = 0;    	$temp29012_ry2 = 0;
    	$temp29012_qg0 = 0;    	$temp29012_qg1 = 0;    	$temp29012_qg2 = 0;
    	$temp29012_naoh0 = 0;   $temp29012_naoh1 = 0;   $temp29012_naoh2 = 0;
    	
    	$temp29013_ry0 = 0;    	$temp29013_ry1 = 0;    	$temp29013_ry2 = 0;   
    	$temp29013_qg0 = 0;    	$temp29013_qg1 = 0;    	$temp29013_qg2 = 0;    
    	$temp29013_naoh0 = 0;   $temp29013_naoh1 = 0;   $temp29013_naoh2 = 0;  
    	
    	$temp29014_ry0 = 0;    	$temp29014_ry1 = 0;    	$temp29014_ry2 = 0;
    	$temp29014_qg0 = 0;    	$temp29014_qg1 = 0;    	$temp29014_qg2 = 0;
    	$temp29014_yry0 = 0;    $temp29014_yry1 = 0;    $temp29014_yry2 = 0;
    	$temp29014_naoh0 = 0;   $temp29014_naoh1 = 0;   $temp29014_naoh2 = 0;
    	
    	$temp29015_ry0 = 0;    	$temp29015_ry1 = 0;    	$temp29015_ry2 = 0;
    	$temp29015_qg0 = 0;    	$temp29015_qg1 = 0;    	$temp29015_qg2 = 0;
    	$temp29015_yry0 = 0;    $temp29015_yry1 = 0;    $temp29015_yry2 = 0;
    	$temp29015_naoh0 = 0;   $temp29015_naoh1 = 0;   $temp29015_naoh2 = 0;
    	
    	$temp29010 = 0 ;
    	$temp29010_1 = 0 ; 
    	
    	$tempArr29010 = [];
    	if($res){
    		foreach ( $res as $k=>&$v)
    		{
    			$v['fbillno'] = $fbillno ;
    			$v['fmodel '] = $fmodel ;
    			$v['fqgyxhl'] =  0;
    			$v['fnaohxhl'] =  0;
    			$v['fniaosuxhl'] =  0 ;
    			$v['fyryxhl'] =  0 ;
    			
    			$timets = 0 ;
    			//燃油消耗量kg）
    			if( in_array($v['fsetkey'],[ 'ffuheyx','fzyqxn', 'fqtyx' ,'fscrglje']) )
    			{
    				//运行时间
    				$timets = $v['fyxtime'] ;
    			}else{
    				//升降时间
    				$timets = $v['fsjtime1'] ;
    			}

				//功率取整
    			$v['fpower'] =  floor($fvalue * $v['ffuheval'] );
    			//最大油耗率（g/kWh）
    			$v['fmaxyh'] =  round( $v['fmaxyhval'] * $v['fyh'] ,2) ;
    			//燃油消耗量kg）
    			$v['fryxhl'] =  round($v['fmaxyh'] * $v['fpower'] * ($timets/$min60) * $jisuan_0_001,2)  ;
    			// 气缸油消耗量（kg）
    			$v['fqgyxhl'] = round($v['fqigangyh'] * $v['fpower'] * ( $timets /$min60) * $jisuan_0_001,2)  ;
    			//引燃油消耗量（kg）
    			$v['fyryxhl'] =   round($v['fyryxh'] * $v['fpower'] * ( $timets /$min60) * $jisuan_0_001 ,2)   ;
    			//NaOH消耗量  fnaoh
    			$v['fnaohxhl'] =  round($v['fnaoh'] * $timets /$min60 ,2); 
    			//尿素消耗量（l） fnsxh
    			$v['fniaosuxhl'] =  round($v['fnsxh'] * $timets /$min60 ,2);
    			
    			// 求和
    			if($v['fobjectid'] == 29011 ){
    				if( in_array($v['fsetkey'], ['ffuheyx',  'ffuhesj']) ) {
    					//燃油消耗量kg）
    					$temp29011_ry += $v['fryxhl']  ;
    					//气缸油消耗量
    					$temp29011_qg += $v['fqgyxhl'] ; 
    				}
    			}
    			
    			//内部提交和 FAT
    			if($v['fobjectid'] == 29012 ){
    				//负荷运行  ， 升降
    				if( in_array($v['fsetkey'], [ 'ffuheyx',  'ffuhesj' ]) ) {
    					$temp29012_ry0 +=  $v['fryxhl'];
    					$temp29012_qg0 +=  $v['fqgyxhl'];
    					$temp29012_naoh0 +=  $v['fnaohxhl'];
    				}
    				//增压器性能
    				if( in_array($v['fsetkey'], [  'fzyqxn'  ]) ) {
    					$temp29012_ry1 +=  $v['fryxhl'];
    					$temp29012_qg1 +=  $v['fqgyxhl'];
    					$temp29012_naoh1 +=  $v['fnaohxhl'];
    				}
    				//其他运行
    				if( in_array($v['fsetkey'], ['fqtyx',  ]) ) {
    					$temp29012_ry2 +=  $v['fryxhl'];
    					$temp29012_qg2 +=  $v['fqgyxhl'];
    					$temp29012_naoh2 +=  $v['fnaohxhl'];
    				}
    				
    			}
    			
    			if($v['fobjectid'] == 29013 ){
    				//负荷运行  ， 升降
    				if( in_array($v['fsetkey'], [ 'ffuheyx',  'ffuhesj' ]) ) {
    					$temp29013_ry0 +=  $v['fryxhl'];
    					$temp29013_qg0 +=  $v['fqgyxhl'];
    					$temp29013_naoh0 +=  $v['fnaohxhl'];
    				}
    				//增压器性能
    				if( in_array($v['fsetkey'], [  'fzyqxn'  ]) ) {
    					$temp29013_ry1 +=  $v['fryxhl'];
    					$temp29013_qg1 +=  $v['fqgyxhl'];
    					$temp29013_naoh1 +=  $v['fnaohxhl'];
    				}
    				//其他运行
    				if( in_array($v['fsetkey'], ['fqtyx',  ]) ) {
    					$temp29013_ry2 +=  $v['fryxhl'];
    					$temp29013_qg2 +=  $v['fqgyxhl'];
    					$temp29013_naoh2 +=  $v['fnaohxhl'];
    				}
    				
    			}
    			
    			
    			if($v['fobjectid'] == 29014 ){
    				//负荷运行  ， 升降
    				if( in_array($v['fsetkey'], [ 'ffuheyx',  'ffuhesj' ]) ) {
    					$temp29014_ry0 +=  $v['fryxhl'];
    					$temp29014_qg0 +=  $v['fqgyxhl'];
    					$temp29014_yry0 +=  $v['fyryxhl'];
    					$temp29014_naoh0 +=  $v['fnaohxhl'];
    				}
    				//增压器性能
    				if( in_array($v['fsetkey'], [  'fzyqxn'  ]) ) {
    					$temp29014_ry1 +=  $v['fryxhl'];
    					$temp29014_qg1 +=  $v['fqgyxhl'];
    					$temp29014_yry1 +=  $v['fyryxhl'];
    					$temp29014_naoh1 +=  $v['fnaohxhl'];
    				}
    				//其他运行
    				if( in_array($v['fsetkey'], ['fqtyx',  ]) ) {
    					$temp29014_ry2 +=  $v['fryxhl'];
    					$temp29014_qg2 +=  $v['fqgyxhl'];
    					$temp29014_yry2 +=  $v['fyryxhl'];
    					$temp29014_naoh2 +=  $v['fnaohxhl'];
    				}
    				
    			}
    			
    			if($v['fobjectid'] == 29015 ){
    				//负荷运行  ， 升降
    				if( in_array($v['fsetkey'], [ 'ffuheyx',  'ffuhesj' ]) ) {
    					$temp29015_ry0 +=  $v['fryxhl'];
    					$temp29015_qg0 +=  $v['fqgyxhl'];
    					$temp29015_yry0 +=  $v['fyryxhl'];
    					$temp29015_naoh0 +=  $v['fnaohxhl'];
    				}
    				//增压器性能
    				if( in_array($v['fsetkey'], [  'fzyqxn'  ]) ) {
    					$temp29015_ry1 +=  $v['fryxhl'];
    					$temp29015_qg1 +=  $v['fqgyxhl'];
    					$temp29015_yry1 +=  $v['fyryxhl'];
    					$temp29015_naoh1 +=  $v['fnaohxhl'];
    				}
    				//其他运行
    				if( in_array($v['fsetkey'], ['fqtyx',  ]) ) {
    					$temp29015_ry2 +=  $v['fryxhl'];
    					$temp29015_qg2 +=  $v['fqgyxhl'];
    					$temp29015_yry2 +=  $v['fyryxhl'];
    					$temp29015_naoh2 +=  $v['fnaohxhl'];
    				}
    			}
    		}
    	}
    	
    	//首台
    	$temp29012_ry +=  ( $temp29012_ry0 *2+$temp29012_ry1 + $temp29012_ry2 ) ;
    	$temp29012_qg += ( $temp29012_qg0*2 + $temp29012_qg1 +$temp29012_qg2 ) ;
    	$temp29012_naoh += ( $temp29012_naoh0*2 +  $temp29012_naoh1 +$temp29012_naoh2) ;
    	//非首台
    	$temp29012_ryno +=  ( $temp29012_ry0 *2  + $temp29012_ry2 ) ;
    	$temp29012_qgno += ( $temp29012_qg0*2   +$temp29012_qg2 ) ;
    	$temp29012_naohno += ( $temp29012_naoh0*2 +$temp29012_naoh2) ;
    	
    	//首台29013
    	$temp29013_ry +=  ( $temp29013_ry0 *2+$temp29013_ry1 + $temp29013_ry2  ) ;
    	$temp29013_qg += ( $temp29013_qg0*2 + $temp29013_qg1 +$temp29013_qg2   ) ;
    	$temp29013_naoh += ( $temp29013_naoh0*2 +  $temp29013_naoh1 +$temp29013_naoh2  ) ;
    	//非首台
    	$temp29013_ryno +=  ( $temp29013_ry0 *2  + $temp29013_ry2   ) ;
    	$temp29013_qgno += ( $temp29013_qg0*2   +$temp29013_qg2  ) ;
    	$temp29013_naohno += ( $temp29013_naoh0*2 +$temp29013_naoh2  ) ;
    	
    	//首台
    	$temp29014_ry +=  ( $temp29014_ry0 *2+$temp29014_ry1 + $temp29014_ry2 ) ;
    	$temp29014_qg += ( $temp29014_qg0*2 + $temp29014_qg1 +$temp29014_qg2 ) ;
    	$temp29014_yry += ( $temp29014_yry0*2 + $temp29014_yry1 +$temp29014_yry2 ) ;
    	$temp29014_naoh += ( $temp29014_naoh0*2 +  $temp29014_naoh1 +$temp29014_naoh2) ;
    	//非首台
    	$temp29014_ryno +=  ( $temp29014_ry0 *2  + $temp29014_ry2 ) ;
    	$temp29014_qgno += ( $temp29014_qg0*2   +$temp29014_qg2 ) ;
    	$temp29014_yryno += ( $temp29014_yry0*2 +$temp29014_yry2 ) ;
    	$temp29014_naohno += ( $temp29014_naoh0*2 +$temp29014_naoh2) ;
   
    	$temp29015_ry +=  ( $temp29015_ry0 *2+$temp29015_ry1 + $temp29015_ry2 ) ;
    	$temp29015_qg += ( $temp29015_qg0*2 + $temp29015_qg1 +$temp29015_qg2 ) ;
    	$temp29015_yry += ( $temp29015_yry0*2 + $temp29015_yry1 +$temp29015_yry2 ) ;
    	$temp29015_naoh += ( $temp29015_naoh0*2 +  $temp29015_naoh1 +$temp29015_naoh2) ;
    	//非首台
    	$temp29015_ryno +=  ( $temp29015_ry0 *2  + $temp29015_ry2 ) ;
    	$temp29015_qgno += ( $temp29015_qg0*2   +$temp29015_qg2 ) ;
    	$temp29015_yryno += ( $temp29015_yry0*2 +$temp29015_yry2 ) ;
    	$temp29015_naohno += ( $temp29015_naoh0*2 +$temp29015_naoh2) ;
//     	dd($temp29011_ry , $temp29012_ry ,$temp29011_ry + $temp29012_ry);
    	
    	//清空值 
    	$db = 'gz_Yjiachun' ; 
    	SqlServerUtil::sqlexecTosql($this->SqlServer, "delete from {$db}   ");
    	SqlServerUtil::sqlsrvAllTosql( $this->SqlServer,$db,$res ) ;
    	
    	//新增每一个 值
    	$data = [
    			'fmoheranyouHZ'=> $temp29011_ry,
    			'fmoheqigangyouHZ'=> $temp29011_qg,
    			//=(内部提交和FAT!W11+内部提交和FAT!W20)/2
    			'temp29011fhry' => round($temp29013_ry0/2 ,2) , //$temp29013_ry0
    			//=(内部提交和FAT!X11+内部提交和FAT!X20)/2
    			'temp29011fhqg' => round($temp29013_qg0/2 ,2),
    			//=内部提交和FAT!W27/2
    			'temp29011qtry' => round($temp29013_ry2/2 ,2),
    			//=内部提交和FAT!X27/2
    			'temp29011qtqg' => round($temp29013_qg2/2 ,2),
    			//=内部提交和FAT!Y29/2 -- 后续
    			'temp29011naoh' => round($temp29013_naohno/2 ,2),
    			
    			//=(内部提交和FAT!AZ11+内部提交和FAT!AZ20)/2
    			'temp29011jcfh' => round($temp29015_ry0/2 ,2),
    			//=(内部提交和FAT!BA11+内部提交和FAT!BA20)/2
    			'temp29011jcfhqg' =>  round($temp29015_qg0/2 ,2),
    			//=内部提交和FAT!AZ27/2
    			'temp29011jcqtry' =>  round($temp29015_ry2/2 ,2),
    			//=内部提交和FAT!BA27/2
    			'temp29011jcqtqg' =>  round($temp29015_qg2/2 ,2),
    			//=内部提交和FAT!BB29/2 -- 后续
    			'temp29011jcyry' =>  round($temp29015_yryno/2 ,2),
    			//=内部提交和FAT!BC29/2 -- 后续
    			'temp29011jcnaoh' =>  round($temp29015_naohno/2 ,2),
    			
    			'fry29012'=>$temp29012_ry ,
    			'fqg29012'=> $temp29012_qg,
    			'fnaoh29012'=> $temp29012_naoh,
    			'fNory29012'=> $temp29012_ryno,
    			'fNoqg29012'=> $temp29012_qgno,
    			'fNonaoh29012'=> $temp29012_naohno,
    			
    			'fry29013'=> $temp29013_ry,
    			'fqg29013'=> $temp29013_qg,
    			'fnaoh29013'=> $temp29013_naoh,
    			
    			'fNory29013'=> $temp29013_ryno,
    			'fNoqg29013'=> $temp29013_qgno,
    			'fNonaoh29013'=> $temp29013_naohno,
    			
    			
    			'fry29014'=>$temp29014_ry ,
    			'fqg29014'=> $temp29014_qg,
    			'fyry29014'=> $temp29014_yry,
    			'fnaoh29014'=> $temp29014_naoh,
    			
    			'fNory29014'=> $temp29014_ryno,
    			'fNoqg29014'=> $temp29014_qgno,
    			'fNoyry29014'=> $temp29014_yryno,
    			'fNonaoh29014'=> $temp29014_naohno,
    			
    			'fry29015'=> $temp29015_ry,
    			'fqg29015'=> $temp29015_qg,
    			'fyry29015'=> $temp29015_yry,
    			'fnaoh29015'=> $temp29015_naoh,
    			'fNory29015'=> $temp29015_ryno,
    			'fNoqg29015'=> $temp29015_qgno,
    			'fNoyry29015'=> $temp29015_yryno,
    			'fNonaoh29015'=> $temp29015_naohno,
    			
    			'fmodel'=> $fmodel,
    			'fbillno'=> $fbillno ,
    	];
    	
    	//各个燃油 气缸 汇总
//     	$db1 = 'gz_YjiachunEach' ; 
//     	SqlServerUtil::sqlexecTosql($this->SqlServer, "delete from {$db1}  where fmodel ='{$fmodel}' and fbillno ='{$fbillno}'  ");
//     	SqlServerUtil::sqlsrvCreate( $this->SqlServer,$db1,$data ) ;
    	
    	//汇总
    	//首机
    	$fobjectid29010 = 29010;
    	//  燃油 isset($tempArr29010['fjiachun'])?$tempArr29010['fjiachun']:
    	$franyou = 0.835 ; // jiachun
    	// 甲醇 
    	$fjiachun = 0.792 ;
    	// 气缸油   isset($tempArr29010['fqigangyou'])?$tempArr29010['fqigangyou']:
    	$fqigangyou = 0.945 ;
 
    	$sjnosj =[
    			'fpower'=> $fvalue,
    			//+磨合调整!U5
    			//=磨合调整!I45+磨合调整!L5+磨合调整!N5+磨合调整!U5+内部提交和FAT!J28+内部提交和FAT!W28+内部提交和FAT!AM28+内部提交和FAT!BB28
    			'fstranyouyjcjEGR'=> $temp29011_ry +$data['temp29011fhry']+ $data['temp29011qtry']+ $data['temp29011jcyry']+ $temp29012_ry +
    				 $temp29013_ry + $temp29014_yry + $temp29015_yry ,
    			//=磨合调整!L5+磨合调整!N5+磨合调整!U5+内部提交和FAT!W28+磨合调整!U5+内部提交和FAT!BB28
    			'fstranyouEGR'=> $data['temp29011fhry']+ $data['temp29011qtry']+ $data['temp29011jcyry']+ $temp29013_ry + $temp29015_yry  ,
    			//=磨合调整!Q5+磨合调整!S5+内部提交和FAT!AK28+内部提交和FAT!AZ28
    			'fstjiachunjijcjEGR'=> $data['temp29011jcfh']+ $data['temp29011jcqtry']+ $temp29014_ry + $temp29015_ry   ,
    			//=磨合调整!Q5+磨合调整!S5+内部提交和FAT!AZ28
    			'fstjiachunjiEGR'=> $data['temp29011jcfh']+ $data['temp29011jcqtry']+ $temp29015_ry     ,
    			//=磨合调整!J45+磨合调整!M5+磨合调整!O5+磨合调整!R5+磨合调整!T5+内部提交和FAT!K28+内部提交和FAT!X28+内部提交和FAT!AL28+内部提交和FAT!BA28
    			'fstqigangjcjEGR'=> $temp29011_qg+ $data['temp29011fhqg']+ $data['temp29011qtqg']+ $data['temp29011jcfhqg']+ $data['temp29011jcqtqg'] +
    				 $temp29012_qg+ $temp29013_qg + $temp29014_qg + $temp29015_qg 	,
    			//=磨合调整!M5+磨合调整!O5+磨合调整!R5+磨合调整!T5+内部提交和FAT!X28+内部提交和FAT!BA28
    			'fstqigangEGR'=> $data['temp29011fhqg']+ $data['temp29011qtqg']+ $data['temp29011jcfhqg']+ $data['temp29011jcqtqg'] +
    			 	 $temp29013_qg  + $temp29015_qg ,
    			//=磨合调整!P5+磨合调整!V5+内部提交和FAT!L28+内部提交和FAT!Y28+内部提交和FAT!AN28+内部提交和FAT!BC28
    			'fstNaOHjcjEGR'=> $data['temp29011naoh']+ $data['temp29011jcnaoh']+ $temp29012_naoh+ $temp29013_naoh+ $temp29014_naoh + $temp29015_naoh,
    			
    			
    			//=磨合调整!I45+磨合调整!L5+磨合调整!N5+磨合调整!U5+内部提交和FAT!J29+内部提交和FAT!W29+磨合调整!U5+内部提交和FAT!AM29+内部提交和FAT!BB29
    			'fnostranyouyjcjEGR'=>$temp29011_ry +$data['temp29011fhry']+ $data['temp29011qtry']+ $data['temp29011jcyry']+ $temp29012_ryno +
    				 $temp29013_ryno + $temp29014_yryno + $temp29015_yryno ,
    			//=磨合调整!L5+磨合调整!N5+磨合调整!U5+内部提交和FAT!W29+磨合调整!U5+内部提交和FAT!BB29
    			'fnostranyouEGR'=>$data['temp29011fhry']+ $data['temp29011qtry']+ $data['temp29011jcyry']+ $temp29013_ryno + $temp29015_yryno ,
    			'fnostjiachunjijcjEGR'=> $data['temp29011jcfh']+ $data['temp29011jcqtry']+ $temp29014_ryno + $temp29015_ryno , 
    			//=磨合调整!Q5+磨合调整!S5+内部提交和FAT!AZ28
    			'fnostjiachunjiEGR'=>$data['temp29011jcfh']+ $data['temp29011jcqtry']+ $temp29015_ryno ,
    			//=磨合调整!J45+磨合调整!M5+磨合调整!O5+磨合调整!R5+磨合调整!T5+内部提交和FAT!K29+内部提交和FAT!X29+内部提交和FAT!AL29+内部提交和FAT!BA29
    			'fnostqigangjcjEGR'=>$temp29011_qg+ $data['temp29011fhqg']+ $data['temp29011qtqg']+ $data['temp29011jcfhqg']+ $data['temp29011jcqtqg'] +
    				$temp29012_qgno+ $temp29013_qgno + $temp29014_qgno + $temp29015_qgno ,
    			'fnostqigangEGR'=>$data['temp29011fhqg']+ $data['temp29011qtqg']+ $data['temp29011jcfhqg']+ $data['temp29011jcqtqg'] +
    				$temp29013_qgno  + $temp29015_qgno  ,
    			//=磨合调整!P5+磨合调整!V5+内部提交和FAT!L29+内部提交和FAT!Y29+内部提交和FAT!AN29+内部提交和FAT!BC29
    			'fnostNaOHjcjEGR'=>$data['temp29011naoh']+ $data['temp29011jcnaoh']+ $temp29012_naohno+ $temp29013_naohno+ $temp29014_naohno + $temp29015_naohno,
    	]+$data;
    	
    	
    	$db2 = 'gz_YjiachunHZ' ;
    	SqlServerUtil::sqlexecTosql($this->SqlServer, "delete from {$db2} where fmodel ='{$fmodel}' and fbillno ='{$fbillno}' ");
    	$ryid = SqlServerUtil::sqlsrvCreateID( $this->SqlServer,$db2,$sjnosj ) ;
    	 
    	$db3 = 'gz_YjiachunHZMX' ;

    	SqlServerUtil::sqlsrvCreate( $this->SqlServer,$db3, $sjnosj + ['finterid'=>$ryid]  ) ;
    	
    	showMsg(0,'成功' ) ;
    	// 29012	SCR主燃油
    	// 29013	ICER主燃油
    	// 29014	引燃油
    	// 29015	燃气
    	 
    	
    }
    
    /**
    * 文件用途描述
    * @date: 2022年12月30日 上午10:09:34
    * @author: juyi
    */
    public function huizonglist(Request $request){
    	$baseSql = "select * from gz_YjiachunEach" ;
    	$res = SqlServerUtil::execToSql($this->SqlServer,  $baseSql); 
    	$result =[];
    	if($res){
    		foreach ($res as $k =>$v )
    		{
    			$result[$v['fsetKey'].'_'.$v['fobjectid']] = $v['fjiachunliang'] ;
    			
    		}
    		
    	}
    	showMsg(0,'成功' , $result) ;
    	
    }
    
    /**
    * 根据型号 获取技术列表信息  
    * @date: 2023年2月1日 下午4:42:54
    * @author: juyi
    */
    public function getJSModelData(Request $request){
    	$fmodel = $request ->fmodel ; //
    	 
    	$fmodelStr = "" ;
    	if ($request->has('fmodel') and $request->fmodel != '' and !empty($request->fmodel)) {
    		$fmodelStr=" and ( charindex('{$fmodel}',v1.Fmodel)>0   )" ;
    	}
    	$CsData = $this->getSystemProfile();
    	$ZhuHunYouPrice = $CsData['fsetzhuhunyouprice'] ;//主滑油单价
    	$QiGangPrice = $CsData['fsetqigangprice'] ;//气缸单价
    	$RanqiPrice = $CsData['fsetranqiprice'] ;//燃气单价
    	$ChaiYouPrice = $CsData['fsetchaiyouprice'] ;//柴油单价
    	$FanYingJiPrice = $CsData['fsetfanyingjiprice'] ;//反应剂的单价
    	$fsetZhuangShiRGprice = $CsData['fsetzhuangshirgprice'] ; //装试人工单价
    	
    	// SCR或者EGR除采购成本以外的成本汇总  （SCR或者EGR油气首台!K2*1+(生效台数-1）*SCR或者EGR油气非首台!K2）/生效台数
    	// ICER其他成本   （icer油气首台!K2*1+(生效台数-1）*icer油气非首台!K2）/生效台数
    	$baseSql = "select top 50 v1.fbrand	,v1.fmodel,v1.fpower,v1.fmoli,v1.fzhuansu,v1.fdieselOil as fjiachunQty,v1.fGas as FranqiQty,v1.fCylinderoil as fqigangyouQty,
		(isnull(v1.fhanjiegs,0)+ isnull(v1.fjijiags,0)+ isnull(v1.fzhuangshigs,0) ) as fbiaozhungs ,isnull(v1.fSCRgs,0)	as fSCRgs, 
		isnull(u1.fqty,0) as fsxQty, {$ZhuHunYouPrice} as FZhuHunYouPrice,{$QiGangPrice} as fQiGangPrice,{$RanqiPrice} as fRanqiPrice,
		{$ChaiYouPrice} as fChaiYouPrice,{$FanYingJiPrice} as fFanYingJiPrice,{$fsetZhuangShiRGprice} as fZhuangShiRGprice,  
		isnull(t1.fAllAmount,0) as fnostAmount , isnull(t2.fAllAmount,0) as fstAmount,
		isnull(t3.fAllAmount,0) as fnostIcerRYAmount ,isnull(t4.fAllAmount,0) as fstIcerRYAmount ,

		Convert(decimal(18,2),round((isnull(t2.fAllAmount,0) + (isnull(u1.fqty,0)- 1)*isnull(t1.fAllAmount,0))/isnull(u1.fqty,1),2)) as fScrEgrHZYouhao,
		Convert(decimal(18,2),round((isnull(t4.fAllAmount,0) + (isnull(u1.fqty,0)- 1)*isnull(t1.fAllAmount,0))/isnull(u1.fqty,1),2)) as fIcerQTCBYouhao
		
		from gz_YBaseJS v1 left join gz_YBaseSC u1 on u1.fmodel = v1.fmodel 
		left join gz_YjzSCREGRNost t1 on  t1.fmodel = v1.fmodel 
		left join gz_YjzSCREGRst t2 on t2.fmodel = v1.fmodel 
		left join gz_YjzICERrybyouqiNOst t3 on t3.fmodel = v1.fmodel 
		left join gz_YjzICERrybyouqist t4 on t4.fmodel = v1.fmodel 
		
		where 1=1 {$fmodelStr} " ;
    	 
    	$res = SqlServerUtil::execToSql($this->SqlServer,  $baseSql);
    	$result =[];
    	
    	if($res){
    		$result = arraykeyToLower($res);
    		foreach ($result as $k1=>&$v1)
    		{ 
    			foreach ($v1 as $k2=>$v2)
    			{
    				if( $v1[$k2] ==='.00' || $v1[$k2]  ==='.0000' || $v1[$k2]  ==='.000000'  ){
    					$v1[$k2] = floatval($v2);
    				}
    			}
    		}
    	}
    	
    	showMsg(0,'成功' , $result) ;
    	
    }
    
    
    /**
    * 文件用途描述
    * @date: 2023年2月6日 下午5:15:13
    * @author: juyi
    */
    public function getjiachunSet(Request $request){
    	$FObjectIDStr = "" ;
    	if ($request->has('fobjectname') and $request->fobjectname != '' and !empty($request->fobjectname)) {
    		$fobjectname = $request->fobjectname  ;
    		$FObjectIDStr=" and ( charindex('{$fobjectname}',u1.FName)>0   )" ;
    	}
    	
    	$fsetKeyNameStr  = "" ;
    	if ($request->has('fsetkey') and $request->fsetkey != '' and !empty($request->fsetkey)) {
    		$fsetKeyName = $request->fsetkey  ;
    		$fsetKeyNameStr=" and ( charindex('{$fsetKeyName}',v1.fsetKey)>0   )" ;
    	}
    	
    	$fkeywordStr  = "" ;
    	if ($request->has('keyword') and $request->keyword != '' and !empty($request->keyword)) {
    		$keyword = $request->keyword  ;
    		$fkeywordStr=" and ( charindex('{$keyword}',v1.fsetKey)>0  or  charindex('{$keyword}',u1.FName)>0  )" ;
    	}
    	
    	$baseSql = " select v1.*,u1.FName as fobjectname  from  gz_YjiachunSet v1 
			left join gz_ObjectType u1 on u1.FObjectID = v1.fobjectid
			where 1=1  {$FObjectIDStr} {$fkeywordStr}  order by v1.fsort   " ;
    	 
    	$res = SqlServerUtil::execTosqlOK($this->SqlServer,  $baseSql);
    	$result =[];
    	
    	if($res){
    		$result = arraykeyToLower($res);
    		foreach ($result as $k1=>&$v1)
    		{
    			foreach ($v1 as $k2=>$v2)
    			{
    				if( $v1[$k2] ==='.00' || $v1[$k2]  ==='.0000'  || $v1[$k2]  ==='.000' || $v1[$k2]  ==='.000000'  ){
    					$v1[$k2] = floatval($v2);
    				}
    				// fnbfhyx
    			}
    			$v1['fsetkeyname'] = getFsetkeyname($v1['fsetkey']);
    		}
    	}
    	
    	showMsg(0,'成功' , $result) ;
    }
    
    /**
    * 文件用途描述
    * @date: 2023年2月7日 上午9:52:54
    * @author: juyi
    */
    public function jiachunSetAdd(Request $request){
    	
    	$FRepKey = $request-> FRepKey;
    	$data = $request->all();
    	$opername =  $request->opername ;
    	 
    	$db = "gz_YjiachunSet";
    	$getVexTableColumnData  =[ 
    		'fobjectid'	=>202,
    		'ffuhe'	=>202,
    		'ffuheval'	=>131,
    		'fmaxyhval'	=>131,
    		'fsetkey'	=>202,
    		'fsjtime'	=>202,
    		'fsort'	=>131,
    		'fyh'	=>131,
    		'fyxtime'	=>202,
    	];
    	// 新增 -- insertRecords,
    	$insertRecords = isset($data['insertRecords'])?$data['insertRecords'] :[];
    	if(!empty($insertRecords)){
    		$insData=[];
    		foreach ($insertRecords as $k =>&$v)
    		{
    			foreach ($getVexTableColumnData as $kup0=> $up)
    			{
    				if( empty($v[$kup0])  && ($up  == 202  ) ){
    					$v[$kup0] = '';
    				}
    				if( empty($v[$kup0])  && ($up  == 131  ) ){
    					$v[$kup0] = 0;
    				}
    				$insData[$k][$kup0] = $v[$kup0] ;
    			}
    		}
//     		dd($insData);
    		SqlServerUtil::sqlsrvAllTosql($this->SqlServer ,$db , $insData);
    		$this->writeLog(getMkName($FRepKey),'新增' , $FRepKey , $insData ,$opername );
    	}
    	// 修改 --  updateRecords,
    	$updateRecords = isset( $data['updateRecords'])?$data['updateRecords'] :[];
    	$fidField = 'fid';
    	 
    	if(!empty($updateRecords)){
    		foreach ($updateRecords as $kup=>&$vup)
    		{
    			$insData =[];
    			$where= [];
    			
    			$where[$fidField] = $vup[$fidField];
    			
    			foreach ($getVexTableColumnData as $kup1=> $up)
    			{
    				if( empty($vup[$kup1])  && ($up  == 202  ) ){
    					$vup[$kup1] = '';
    				}
    				if( empty($vup[$kup1])  && ($up  == 131  ) ){
    					$vup[$kup1] = 0;
    				}
    				$insData[$kup1] = $vup[$kup1] ;
    			}
    			 
    			SqlServerUtil::exeupdateTosql($this->SqlServer,$db,$insData,$where);
    			$this->writeLog(getMkName($FRepKey),'修改' , $FRepKey , $insData ,$opername );
    		}
    		
    	}
    	//删除 --  removeRecords
    	$removeRecords = isset( $data['removeRecords'])?$data['removeRecords'] :[];
    	if(!empty($removeRecords)){
    		$Fids =[];
    		$Fids = join("','", array_column($removeRecords, 'fid'));
    		// 			dd($Fids);
    		SqlServerUtil::sqlsrvDelete($this->SqlServer,$db, ['fid'=>"'".$Fids ."'" ],true);
    		$this->writeLog(getMkName($FRepKey),'删除' , $FRepKey , $removeRecords ,$opername );
    	}
    	showMsg(0,'成功' );
    }
    
    /**
    * 文件用途描述
    * @date: 2023年4月28日 下午7:05:03
    * @author: juyi
    */
    function  jiachunYouHao(Request $request){
    	$FRepKey = $request-> FRepKey ;
    	$data = $request->all() ;
    	$opername =  $request->opername ;
    	$fbillno = $data['fbillno'] ;
    	$db = "gz_YjzJiaChunQTYouHao" ;
    	
    	$baseSql ="select  v1.fmodel as  N'机型',v1.fqty as N'生效台数',v1.fstranyouyjcjegr as  N'首台燃油/引燃油EGR(含)',
		v1.fsetchaiyouprice as  N'燃油单价',v1.fstjiachunjijcjegr as  N'首台甲醇EGR(含)',v1.fsetjiachunprice as  N'甲醇单价'
,v1.fstqigangjcjegr as  N'首台气缸油EGR(含)',v1.fsetqigangprice as  N'气缸单价',v1.fstamount as  N'首台汇总'
,v1.fnostranyouyjcjegr as  N'非首台燃油/引燃油EGR(含)',v1.fnostjiachunjijcjegr as  N'非首台甲醇EGR(含)',v1.fnostqigangjcjegr as  N'非首台气缸油EGR(含)'
,v1.fnostamount as  N'非首台汇总'  
    	from gz_YjzJiaChunQTYouHao  v1 where v1.fbillno = '{$fbillno}'  " ;
    	
    	$res = SqlServerUtil::execTosqlOKTO($this->SqlServer,  $baseSql);
    	
    	showMsg(0,'成功',$res );
    }
    
    
    
    
	

}
